package com.beself.controller;

import com.beself.entity.TMine;
import com.beself.entity.TProblem;
import com.beself.service.MineService;
import com.beself.until.RestJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("api/mine")
public class MineController {

    @Resource
    private MineService mineService;

    @Resource
    private RestJson restJson;

    /**
     * 根据用户的序号查询  指定用户的信息
     *
     * @param uId
     * @return
     */
    @RequestMapping("/findMine")
    @ResponseBody
    public RestJson findMine(@RequestParam("uId") Integer uId) {

        TMine tMine = mineService.findMine(uId);
        if (tMine != null) {
            restJson = new RestJson<>(true, "成功", tMine);
        } else {
            restJson = new RestJson<>(false, "失败", null);
        }
        return restJson;
    }

    /**
     * 用户信息更新
     */
    @RequestMapping("updateMine")
    @ResponseBody
    public RestJson updateMine(TMine tMine, @RequestParam("uploadname") MultipartFile headimg, HttpServletRequest request) {
        String filename = "";
        if (headimg != null) {
            String realPath = request.getRealPath("/upload");
            File file = new File(realPath);
//            if (!file.exists()){
//                file.mkdirs();
//            }
            System.out.println(realPath);
            filename = headimg.getOriginalFilename();
            if (filename != null && filename.trim().length() > 0) {
                tMine.setmImg(filename);
            }
            System.out.println(filename);
            try {
                headimg.transferTo(new File(file + "/" + filename));

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        boolean flag = mineService.updateMine(tMine);

        if (flag) {
            restJson = new RestJson(true, "成功", null);
        } else {
            restJson = new RestJson(false, "失败", null);
        }
        return restJson;
    }

    /**
     * 反馈信息添加
     *
     * @param tProblem
     * @return
     */
    @RequestMapping("insertProblem")
    @ResponseBody
    public RestJson insertProblem(TProblem tProblem, @RequestParam("updatename") MultipartFile headimg, HttpServletRequest request) {
        String filename = "";
        if (headimg != null) {
            String realPath = request.getRealPath("/upload");
            File file = new File(realPath);
//            if (!file.exists()){
//                file.mkdirs();
//            }
            System.out.println(realPath);
            filename = headimg.getOriginalFilename();
            System.out.println(filename);
            try {
                headimg.transferTo(new File(file + "/" + filename));

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Boolean flag = mineService.insertProblem(tProblem);

        if (flag) {
            restJson = new RestJson(true, "成功", null);
        } else {
            restJson = new RestJson(false, "失败", null);
        }
        return restJson;
    }

    /**
     * 普通账号----->会员账号
     */
    public RestJson insertMine(TMine mine) {

        return null;
    }

    //会员开通  信息展示
    @RequestMapping("showSuperUser")
    @ResponseBody
    public RestJson showSuperUser(TMine tMine) {

      TMine mine =   mineService.showSuperUser(tMine);
        if (mine != null) {
            restJson = new RestJson<>(true, "成功", mine);
        } else {
            restJson = new RestJson<>(false, "失败", null);
        }
        return restJson;

    }

}
